<%--
  Created by IntelliJ IDEA.
  User: dzw
  Date: 2017/12/12
  Time: 16:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";

%>

<style>
  .entityDiv{
    margin: 5px 0 20px 0;
    padding-left: 45px;
  }
  .moduleLabel{
    color: #2b425b;
  }
</style>

<div id="page-content">
  <div class="row">

    <div class="col-lg-12">
      <div class="panel">
        <div class="panel-heading">
          <h3 class="panel-title">
            <c:if test="${modulePower==null}">模块查看权限和表单新建权限申请</c:if>
            <c:if test="${reApply==1}">再次申请权限</c:if>
            <c:if test="${modulePower.processState=='Backed'}"><span style="color: indianred">您的申请未通过，请重新申请</span></c:if>
          </h3>
        </div>

        <c:if test="${modulePower==null || modulePower.processState=='Backed' || reApply==1}">
        <form id="form_modulePower_input" class="form-horizontal">
          <input type="hidden" name="keyId" id="keyId" value="${modulePower.id}"/>
          <input type="hidden" name="reApply" id="reApply" value="${reApply}">
          <div class="panel-body" id="powerDiv">

              <%--老的--%>
              <%--<c:forEach var="d" items="${data}" varStatus="dStatus">--%>
                <%--<div class="checkbox">--%>
                  <%--<input type="checkbox" id="module${dStatus.index}" class="magic-checkbox" name="module" value="${d.moduleName}" <c:if test="${fn:indexOf(modulePower.moduleName,d.moduleName)>=0}">checked</c:if> >--%>
                  <%--<label class="moduleLabel" for="module${dStatus.index}">${d.moduleName}</label>--%>
                  <%--<div class="entityDiv">--%>
                    <%--<c:if test="${fn:indexOf(modulePower.moduleName,d.moduleName)>=0}">--%>
                    <%--<c:forEach var="e" items="${d.entityMap}" varStatus="eStatus">--%>
                      <%--<input type="checkbox" id="${e.entity.key}entity${eStatus.index}" class="magic-checkbox" name="entity" value="${e.entity.key}" <c:if test="${fn:indexOf(modulePower.entityName,e.entity.key)>=0}">checked</c:if> >--%>
                      <%--<label class="entityLabel" for="${e.entity.key}entity${eStatus.index}">${e.entity.value}</label>--%>
                    <%--</c:forEach>--%>
                    <%--</c:if>--%>
                  <%--</div>--%>
                <%--</div>--%>
              <%--</c:forEach>--%>

                <%--模块--%>
                <div class="col-md-6 col-xs-6" style="padding: 0;">
                  <ul class="list-group">
                    <c:forEach var="d" items="${data}" varStatus="dStatus">
                      <li class="list-group-item">
                        <input type="checkbox" id="module${dStatus.index}" class="magic-checkbox" name="module" value="${d.moduleName}" <c:if test="${fn:indexOf(modulePower.moduleName,d.moduleName)>=0}">checked</c:if> >
                        <label class="moduleLabel" for="module${dStatus.index}">${d.moduleName}</label>
                      </li>
                    </c:forEach>
                  </ul>
                </div>

                <%--表单--%>
                <div class="col-md-6 col-xs-6" style="padding: 0">
                  <ul class="list-group" id="entityUl">
                    <c:forEach var="d" items="${data}" varStatus="dStatus">
                      <c:if test="${fn:indexOf(modulePower.moduleName,d.moduleName)>=0}">
                        <c:forEach var="e" items="${d.entityMap}" varStatus="eStatus">
                          <li class="list-group-item module${dStatus.index}">
                            <input type="checkbox" id="${e.entity.key}entity${eStatus.index}" class="magic-checkbox" name="entity" value="${e.entity.key}" <c:if test="${fn:indexOf(modulePower.entityName,e.entity.key)>=0}">checked</c:if> >
                            <label class="entityLabel" for="${e.entity.key}entity${eStatus.index}">${d.moduleName} - ${e.entity.value}</label>
                          </li>
                        </c:forEach>
                      </c:if>
                    </c:forEach>
                  </ul>
                </div>

          </div>
          <div class="panel-footer">
            <a class="btn btn-warning" id="btn-commit-common" href="javascript:void(0);"><i class="fa fa-upload"></i> 提交 </a>
          </div>
        </form>
        </c:if>

        <c:if test="${modulePower.processState=='Running'}">
          <div class="row" style="height: 600px">
            <div class="col-lg-6 col-lg-offset-3">
              <div class="panel panel-trans text-center">
                <h3 class="panel-title" style="margin-top: 250px;color: #9da9b6">您的权限申请正在审批中，请等待...</h3>
              </div>
            </div>
          </div>
        </c:if>

      </div>
    </div>

  </div>
</div>

<script>

//  老的
  <%--$(":checkbox[name='module']").click(function(){--%>
    <%--var entityContainer=$(this).parent().find(".entityDiv");--%>
    <%--if($(this).is(':checked')){--%>
      <%--var url="<%=path%>/com/modulePower/getFormList";--%>
      <%--__ajax_get(url,{moduleName:$(this).val()},function(data){--%>
        <%--entityContainer.empty();--%>
        <%--for(var i=0;i<data.length;i++){--%>
          <%--var newCheckBox= '<input type="checkbox" id="entity'+i+data[i].key+'" class="magic-checkbox" name="entity" value="'+data[i].key+'" >'--%>
                  <%--+'<label for="entity'+i+data[i].key+'">'+data[i].value+'</label>';--%>
          <%--entityContainer.append(newCheckBox);--%>
        <%--}--%>
      <%--})--%>
    <%--}else{--%>
      <%--entityContainer.empty();--%>
    <%--}--%>
  <%--});--%>

  $(":checkbox[name='module']").click(function(){
    var cName=$(this).attr("id");
    var mName=$(this).val();
    if(!$(this).is(':checked')){
      $("."+cName+"").remove();
    }else{
      if($("."+cName+"").size()<=0){
        __ajax_get("<%=path%>/com/modulePower/getFormList",{moduleName:mName},function(data){
          for(var i=0;i<data.length;i++){
            var newLi='<li class="list-group-item '+cName+'">'
                    +'<input type="checkbox" id="'+data[i].key+'entity'+i+'" class="magic-checkbox" name="entity" value="'+data[i].key+'">'
                    +'<label class="entityLabel" for="'+data[i].key+'entity'+i+'">'+mName+' - '+data[i].value+'</label>'
                    +'</li>';
            $("#entityUl").append(newLi);
          }
        })
      }
    }
  });

  //提交
  $("#btn-commit-common,#btn-recommit-common").click(function(){

    if(!$(":checked[name='module']").val()){
      __toastr_error("至少选择一个模块");
      return false;
    }

    if(!$(":checked[name='entity']").val()){
      __toastr_error("至少选择一个表单");
      return false;
    }

    __confirm_dialog("提示","确定提交吗？",function(){
      __form_save("form_modulePower_input", "<%=path%>/com/modulePower/commit",function(data){
        if(data.state==1){
          $(".panel-heading").empty();
          $("#form_modulePower_input").empty();
          $("#form_modulePower_input").html('<div class="row" style="height: 600px">'
              +'<div class="col-lg-6 col-lg-offset-3">'
              +'<div class="panel panel-trans text-center">'
              +'<h3 class="panel-title" style="margin-top: 250px;color: #9da9b6">您的权限申请正在审批中，请等待...</h3>'
              +'</div>'
              +'</div>'
              +'</div>');
        }
      })
    })
  });


</script>